package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.markingsystem.SubjectAnswerActivity;
import com.everflourish.yeah100.entity.marking.SubjectAnswer;
import com.everflourish.yeah100.entity.marking.Topic;
import com.everflourish.yeah100.utils.StringUtil;

import java.util.List;

/**
 * 作者：Created by liqisi on 2016/10/25.
 * 时间：2016/10/25
 */
public class SubjectAnswerAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    public Context mContext;
    private List<SubjectAnswer> mList;
    public SubjectAnswerActivity mActivity;

    public SubjectAnswerAdapter(Context mContext, SubjectAnswerActivity mActivity, List<SubjectAnswer> mList) {
        this.mInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.mList = mList;
//        this.mActivity = mActivity;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SubjectAnswer subjectAnswer = mList.get(position);
        HolderView holderView = null;
        if (convertView == null) {
            holderView = new HolderView();
            convertView = mInflater.inflate(R.layout.item_subject_answer,
                    parent, false);
            holderView.serialNo = (TextView) convertView
                    .findViewById(R.id.item_subject_serial);
            holderView.score = (TextView) convertView
                    .findViewById(R.id.item_subject_score);
            convertView.setTag(holderView);
        } else {
            holderView = (HolderView) convertView.getTag();
        }
        String answer = subjectAnswer.getAnswer();
        Topic topic = subjectAnswer.getTopic();
        int beginNo = topic.getTopicBegin();
        int endNo = topic.getTopicEnd();
        if (answer.equals("")) {
            answer = "0";
        }
        if (beginNo != endNo) {
            holderView.serialNo.setText(topic.getTopicBegin() + "~" + topic.getTopicEnd() + "题分数");
        } else {
            holderView.serialNo.setText(topic.getTopicBegin() + "题分数");
        }
        if (TextUtils.isEmpty(answer) || answer.equals(" ") || answer.equals("")) {
            holderView.score.setText("0.0");
        } else if (answer.equals("0.0")) {
            holderView.score.setText("0.0");
        } else {
            holderView.score.setText(StringUtil.parseFloat(answer, 0f) + "");
        }
        return convertView;
    }

    class HolderView {
        public TextView serialNo = null;
        public TextView score = null;
    }
}
